package exam.service.impl;

import com.github.pagehelper.PageHelper;
import exam.bean.Teacher;
import exam.dao.TeacherDao;
import exam.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public List<Teacher> selectTeacher(Map<String, Object> queryMap) {
        // 开始分页
        PageHelper.startPage((Integer) queryMap.get("pageNum"), (Integer) queryMap.get("pageSize"));
        
        // 处理查询参数，移除空值
        Map<String, Object> cleanQueryMap = new HashMap<>();
        
        String name = (String) queryMap.get("name");
        if (name != null && !name.trim().isEmpty()) {
            cleanQueryMap.put("name", name.trim());
        }
        
        String phoneNumber = (String) queryMap.get("phoneNumber");
        if (phoneNumber != null && !phoneNumber.trim().isEmpty()) {
            cleanQueryMap.put("phoneNumber", phoneNumber.trim());
        }
        
        String subject = (String) queryMap.get("subject");
        if (subject != null && !subject.trim().isEmpty()) {
            cleanQueryMap.put("subject", subject.trim());
        }
        
        // 执行查询
        return teacherDao.selectTeacher(cleanQueryMap);
    }

    @Override
    public int insertTeacher(Teacher teacher) {
        return teacherDao.insertTeacher(teacher);
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        return teacherDao.updateTeacher(teacher);
    }

    @Override
    public int deleteTeacher(int id) {
        return teacherDao.deleteTeacher(id);
    }

    @Override
    public Teacher selectTeacherById(Map<String, Object> map) {
        return teacherDao.selectTeacher(map).get(0);
    }
}
